/**
 * Created by 卡西莫多 on 2017/6/6.
 */
import {Injectable} from '@angular/core';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import {Cart} from "../models/Cart";
import {Dish} from "../models/Dish";
import {Http} from "@angular/http";
import {url_cart as api_url, url_store} from "../config";
import {CustomerProvider} from "./customerProvider";
import {Observable} from "rxjs/Observable";

import {EmployeeProvider} from "./employeeProvider";

/*
 Generated class for the DishProvider provider.

 See https://angular.io/docs/ts/latest/guide/dependency-injection.html
 for more info on providers and Angular 2 DI.
 */
@Injectable()
export class CartProvider {
  private carts: Cart[];
  total = 0.00;

  Stores: {
    Store_ID: String,
    Tables: String[],
  } [];

  constructor(public http: Http,
              public customer: CustomerProvider,
              public employee: EmployeeProvider) {
  }

  getCart(): Observable<any> {
    let link = api_url + '?';

    if (this.employee.check())
      link = link + 'Waiter_Employee_ID=' + this.employee.employeeId() + '&Store_ID=' + this.Store_ID() + '&Table_ID=' + this.Table_ID();
    else
      link = link + 'Customer_ID=' + this.customer.userId();

    return this.http.get(link)
      .map(res => {
        return this.response(res.json())
      });
  }

  changeCart(dish: Dish, num = 1): Observable<any> {
    let link = api_url;
    if (link != null) {
      let user_id = this.customer.userId();
      let employee_id = this.employee.employeeId();
      let cart = new Cart(null, user_id, employee_id, null, null, dish.Dish_ID, dish.Dish_Name, dish.Dish_Abbreviation, dish.Price, dish.Dish_Category_ID, num, null, this.Store_ID(), this.Table_ID(), null);
      return this.http.post(link, JSON.stringify(cart)).map(res => this.response(res.json()));
    }
  }

  clearCart(): Observable<any> {
    let link = api_url + '/clear?';

    if (this.employee.check())
      link = link + 'Waiter_Employee_ID=' + this.employee.employeeId() + '&Store_ID=' + this.Store_ID() + '&Table_ID=' + this.Table_ID();
    else
      link = link + 'Customer_ID=' + this.customer.userId();

    return this.http.get(link).map(res => {
      this.carts = null;
      this.total = 0.00;
      return res.json();
    });
  }

  private response(res: any): Observable<any> {
    if (res.success) {
      this.carts = res.data as Cart[];
      this.calcTotal();
    }
    return res;
  }

  private calcTotal() {
    this.total = 0.00;
    if (this.carts != null)
      for (let c of this.carts) {
        this.total = this.total + c.Price * c.Number_of_Orders;
      }
    console.log('Cart Provider', this.total);
    return this.total;
  }

  public toMap(): any {
    let carts = this.carts;
    let map = new Map();
    if (carts)
      for (let c of carts) {
        map.set(c.Dish_ID.toString(), c);
      }
    return map;
  }

  Store_ID(store_id?: string) {
    if (store_id != null)
      localStorage.setItem('Store_ID', store_id);
    return localStorage.getItem('Store_ID');
  }

  Table_ID(table_id?: string) {
    if (table_id != null)
      localStorage.setItem('Table_ID', table_id);
    return localStorage.getItem('Table_ID');
  }


  getTables() {
    return this.http.get(url_store)
      .map(res => {
        this.Stores = res.json().data;
        console.log('cartProd:', this.Stores);
        return this.Stores;
      })
  }

}
